<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="广告用户ID" prop="userid">
        <el-input
          v-model="queryParams.userid"
          placeholder="请输入广告用户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="位置标识" prop="adPosition">
        <el-input
          v-model="queryParams.adPosition"
          placeholder="请输入位置标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="广告类型(0,图片,1,文字,2,视频)" prop="adType">
        <el-select clearable v-model="queryParams.adType" placeholder="请选择广告类型(0,图片,1,文字,2,视频)" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="广告类型标识" prop="adTypeCode">
        <el-input
          v-model="queryParams.adTypeCode"
          placeholder="请输入广告类型标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="广告宽度" prop="adWidth">
        <el-input
          v-model="queryParams.adWidth"
          placeholder="请输入广告宽度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="广告高度" prop="adHeight">
        <el-input
          v-model="queryParams.adHeight"
          placeholder="请输入广告高度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="广告过期时间" prop="expirationTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.expirationTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择广告过期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="广告预算金额" prop="adMoney">
        <el-input
          v-model="queryParams.adMoney"
          placeholder="请输入广告预算金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图片个数" prop="imgCount">
        <el-input
          v-model="queryParams.imgCount"
          placeholder="请输入图片个数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="广告权重" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入广告权重"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="点击量" prop="clickNum">
        <el-input
          v-model="queryParams.clickNum"
          placeholder="请输入点击量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select clearable v-model="queryParams.status" placeholder="请选择审核状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="展示次数" prop="showNum">
        <el-input
          v-model="queryParams.showNum"
          placeholder="请输入展示次数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="广告计费方式" prop="extend1">
        <el-input
          v-model="queryParams.extend1"
          placeholder="请输入广告计费方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="广告阅读次数" prop="extend2">
        <el-input
          v-model="queryParams.extend2"
          placeholder="请输入广告阅读次数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段3" prop="extend3">
        <el-input
          v-model="queryParams.extend3"
          placeholder="请输入扩展字段3"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段4" prop="extend4">
        <el-input
          v-model="queryParams.extend4"
          placeholder="请输入扩展字段4"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段5" prop="extend5">
        <el-input
          v-model="queryParams.extend5"
          placeholder="请输入扩展字段5"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:appAdv:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:appAdv:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:appAdv:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:appAdv:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appAdvList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="广告编号" align="center" prop="id" v-if="false"/>
      <el-table-column label="广告用户ID" align="center" prop="userid" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="位置标识" align="center" prop="adPosition" />
      <el-table-column label="广告类型(0,图片,1,文字,2,视频)" align="center" prop="adType" />
      <el-table-column label="广告类型标识" align="center" prop="adTypeCode" />
      <el-table-column label="广告宽度" align="center" prop="adWidth" />
      <el-table-column label="广告高度" align="center" prop="adHeight" />
      <el-table-column label="广告过期时间" align="center" prop="expirationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expirationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="广告内容" align="center" prop="content" />
      <el-table-column label="广告链接地址" align="center" prop="adUrl" />
      <el-table-column label="图片url" align="center" prop="imgUrl" />
      <el-table-column label="广告预算金额" align="center" prop="adMoney" />
      <el-table-column label="图片个数" align="center" prop="imgCount" />
      <el-table-column label="广告权重" align="center" prop="weight" />
      <el-table-column label="点击量" align="center" prop="clickNum" />
      <el-table-column label="审核状态" align="center" prop="status" />
      <el-table-column label="展示次数" align="center" prop="showNum" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="广告计费方式" align="center" prop="extend1" />
      <el-table-column label="广告阅读次数" align="center" prop="extend2" />
      <el-table-column label="扩展字段3" align="center" prop="extend3" />
      <el-table-column label="扩展字段4" align="center" prop="extend4" />
      <el-table-column label="扩展字段5" align="center" prop="extend5" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:appAdv:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:appAdv:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改APP内广告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="广告用户ID" prop="userid">
          <el-input v-model="form.userid" placeholder="请输入广告用户ID" />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="位置标识" prop="adPosition">
          <el-input v-model="form.adPosition" placeholder="请输入位置标识" />
        </el-form-item>
        <el-form-item label="广告类型(0,图片,1,文字,2,视频)" prop="adType">
          <el-select clearable v-model="form.adType" placeholder="请选择广告类型(0,图片,1,文字,2,视频)">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="广告类型标识" prop="adTypeCode">
          <el-input v-model="form.adTypeCode" placeholder="请输入广告类型标识" />
        </el-form-item>
        <el-form-item label="广告宽度" prop="adWidth">
          <el-input v-model="form.adWidth" placeholder="请输入广告宽度" />
        </el-form-item>
        <el-form-item label="广告高度" prop="adHeight">
          <el-input v-model="form.adHeight" placeholder="请输入广告高度" />
        </el-form-item>
        <el-form-item label="广告过期时间" prop="expirationTime">
          <el-date-picker clearable size="small"
            v-model="form.expirationTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择广告过期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="广告预算金额" prop="adMoney">
          <el-input v-model="form.adMoney" placeholder="请输入广告预算金额" />
        </el-form-item>
        <el-form-item label="图片个数" prop="imgCount">
          <el-input v-model="form.imgCount" placeholder="请输入图片个数" />
        </el-form-item>
        <el-form-item label="广告权重" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入广告权重" />
        </el-form-item>
        <el-form-item label="点击量" prop="clickNum">
          <el-input v-model="form.clickNum" placeholder="请输入点击量" />
        </el-form-item>
        <el-form-item label="审核状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="展示次数" prop="showNum">
          <el-input v-model="form.showNum" placeholder="请输入展示次数" />
        </el-form-item>
        <el-form-item label="默认0未删除，1已删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入默认0未删除，1已删除" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="广告计费方式" prop="extend1">
          <el-input v-model="form.extend1" placeholder="请输入广告计费方式" />
        </el-form-item>
        <el-form-item label="广告阅读次数" prop="extend2">
          <el-input v-model="form.extend2" placeholder="请输入广告阅读次数" />
        </el-form-item>
        <el-form-item label="扩展字段3" prop="extend3">
          <el-input v-model="form.extend3" placeholder="请输入扩展字段3" />
        </el-form-item>
        <el-form-item label="扩展字段4" prop="extend4">
          <el-input v-model="form.extend4" placeholder="请输入扩展字段4" />
        </el-form-item>
        <el-form-item label="扩展字段5" prop="extend5">
          <el-input v-model="form.extend5" placeholder="请输入扩展字段5" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAppAdv, getAppAdv, delAppAdv, addAppAdv, updateAppAdv, exportAppAdv } from "@/api/business/appAdv";

export default {
  name: "AppAdv",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // APP内广告表格数据
      appAdvList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userid: undefined,
        title: undefined,
        adPosition: undefined,
        adType: undefined,
        adTypeCode: undefined,
        adWidth: undefined,
        adHeight: undefined,
        expirationTime: undefined,
        content: undefined,
        adUrl: undefined,
        imgUrl: undefined,
        adMoney: undefined,
        imgCount: undefined,
        weight: undefined,
        clickNum: undefined,
        status: undefined,
        showNum: undefined,
        extend1: undefined,
        extend2: undefined,
        extend3: undefined,
        extend4: undefined,
        extend5: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        extend1: [
          { required: true, message: "广告计费方式不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询APP内广告列表 */
    getList() {
      this.loading = true;
      listAppAdv(this.queryParams).then(response => {
        this.appAdvList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userid: undefined,
        title: undefined,
        adPosition: undefined,
        adType: undefined,
        adTypeCode: undefined,
        adWidth: undefined,
        adHeight: undefined,
        expirationTime: undefined,
        content: undefined,
        adUrl: undefined,
        imgUrl: undefined,
        adMoney: undefined,
        imgCount: undefined,
        weight: undefined,
        clickNum: undefined,
        status: 0,
        updateTime: undefined,
        createTime: undefined,
        showNum: undefined,
        delFlag: undefined,
        remark: undefined,
        extend1: undefined,
        extend2: undefined,
        extend3: undefined,
        extend4: undefined,
        extend5: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加APP内广告";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAppAdv(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改APP内广告";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAppAdv(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppAdv(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除APP内广告编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAppAdv(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有APP内广告数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAppAdv(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
